
rm(list=ls())#clear Global Environment
setwd('./Fig.1G_metabolics_volcano')


library(ggplot2) # Create Elegant Data Visualisations Using the Grammar of Graphics
library(dplyr) # A Grammar of Data Manipulation
library(ggfittext) # Fit Text Inside a Box in 'ggplot2'
library(ggrepel) # Automatically Position Non-Overlapping Text Labels with 'ggplot2'
library(RColorBrewer) # ColorBrewer Palettes
library(grid) # The Grid Graphics Package
library(scales) # Scale Functions for Visualization


df<-read.xlsx("data_metabolitics.xlsx", sheet=1,check.names = F, sep = "\t")

head(df)
df$group <- factor(df$group, levels = c("DR_R", "R_OR", "OR_OY",
                                        "OY_Y", "Y_DR"))

df$group2<-as.factor(ifelse(df$p_value < 0.05 & abs(df$log2FC) >= 2, 
                           ifelse(df$log2FC>= 2 ,'up','down'),'NS'))
df$group2 <- factor(df$group2, levels = c("up", "down", "NS"))

df$label<-ifelse(df$p_value<0.05&abs(df$log2FC)>=4,"Y","N")
df$label<-ifelse(df$label == 'Y', as.character(df$Name), '')

df$G <- ifelse(df$log2FC>=0, "Sigdown","Sigup")

df

df_bg <- df %>%
  group_by(group) %>%
  summarize(max_log2FC = max(log2FC),min_log2FC = min(log2FC))


View(df_bg)

p <- ggplot()+

  geom_col(data = df_bg, 
           mapping = aes(group=group,max_log2FC),
           fill = "grey85", width = 0.8, alpha = 0.5) +

  geom_col(data = df_bg, 
           mapping = aes(group=group, min_log2FC),
           fill = "grey85", width = 0.8, alpha = 0.5)
p

df_bg

p1 <- p+geom_jitter(data = df,
                   mapping = aes(x = group, y = log2FC, color = group2),
                   size= 3,width =0.4, alpha = 0.7)
p1


p2 <- p1+geom_col(data = df_bg,
                  mapping = aes(x= group, y = 0.3, fill = group))+
  geom_col(data = df_bg,
           mapping = aes(x= group, y = -0.3, fill = group))
p2



p3 <- p2+geom_text(data=df_bg,
                   mapping = aes(x=group, y=0, label=group),
                   size = 4, color ="#dbebfa")
p3


ggplot()+

  geom_col(data = df_bg, 
           mapping = aes(group,max_log2FC),
           fill = "grey90", width = 0.8, alpha = 0.5) +

  geom_col(data = df_bg, 
           mapping = aes(group, min_log2FC),
           fill = "grey90", width = 0.8, alpha = 0.5)+

  geom_jitter(data = df,
              mapping = aes(x = group, y = log2FC, color = G),
              size= 3,width = 0.4, alpha = 0.9)+

  geom_col(data = df_bg,
           mapping = aes(x= group, y = 0.6, fill = group),
           width = 0.8, show.legend = F)+
  geom_col(data = df_bg,
           mapping = aes(x= group, y = -0.6, fill = group),
           width = 0.8, show.legend = F)+

  geom_fit_text(data=df_bg,
                mapping = aes(x=group, y=0, label=group),
                size = 20, color ="black")+

  coord_polar(direction=1)+

  geom_text_repel(data = df,
                  mapping = aes(x = group, y = log2FC, label = label),
                  max.overlaps = 10000,
                  size=3,
                  box.padding=unit(0.3,'lines'),
                  point.padding=unit(0.3, 'lines'),
                  segment.color='black',
                  show.legend=FALSE)+

  theme_void()+

  
  scale_fill_manual(values = c("DR_R"="#0ebeff", "R_OR"="#47cf73", "OR_OY"="#ae63e4",
                               "OY_Y"="#fcd000", "Y_DR"="#ff3c41"))+
  scale_color_manual(values = c("#a4c639","#b52e31"),name=NULL)+
  guides(
    color = guide_legend(
      override.aes = list(size = 7, shape = 20)))

#color <- colorRampPalette(brewer.pal(11,"BrBG"))(30)

grid.raster(alpha(color, 0.2), 
            width = unit(1, "npc"), 
            height = unit(1,"npc"),
            interpolate = T)


vanno<-ggplot()+

  geom_col(data = df_bg, 
           mapping = aes(group,max_log2FC),
           fill = "grey90", width = 0.8, alpha = 0.5) +

  geom_col(data = df_bg, 
           mapping = aes(group, min_log2FC),
           fill = "grey90", width = 0.8, alpha = 0.5)+
 
  geom_jitter(data = df,
              mapping = aes(x = group, y = log2FC, color = group2),
              size= 3,width = 0.4, alpha = 0.5)+

  geom_col(data = df_bg,
           mapping = aes(x= group, y = 0.6, fill = group),
           width = 0.8, show.legend = F)+
  geom_col(data = df_bg,
           mapping = aes(x= group, y = -0.6, fill = group),
           width = 0.8, show.legend = F)+

  geom_fit_text(data=df_bg,
                mapping = aes(x=group, y=0, label=group),
                size = 20, color ="black")+

  coord_polar(direction=1)+

  geom_text_repel(data = df,
                  mapping = aes(x = group, y = log2FC, label = label),
                  max.overlaps = 10000,
                  size=3,
                  box.padding=unit(0.3,'lines'),
                  point.padding=unit(0.3, 'lines'),
                  segment.color='black',
                  show.legend=FALSE)+
  theme_void()+
  scale_fill_manual(values = c("DR_R"="#0ebeff", "R_OR"="#47cf73", "OR_OY"="#ae63e4",
                               "OY_Y"="#fcd000", "Y_DR"="#ff3c41"))+
  scale_color_manual(values = c("#e42313", "#0061d5", "#8b8c8d"),name=NULL)+
  guides(
    color = guide_legend(
      override.aes = list(size = 7, shape = 20)))

ggsave("1vanno.svg",vanno)


